import {getCart,gettotalPay,getProdCount,getChageItem,getdeleteItem,} from "@/services";

const state = {
  cartData: [],
  tatalPayData: {},
  checkAllFlag: false,
  TotalCont: 0,
};
const getters = {};
const mutations = {
  update(state, paylod) {
    state.cartData = paylod.shopCartItemDiscounts[0].shopCartItems;
    state.cartData.map((item) => {
      item.flag = false;
      return item;
    });
  },
  gettatalPayData(state, payload) {
    state.tatalPayData = payload;
  },
  setcartData(state, payload) {
    // 单选框
    state.cartData.forEach((item) => {
      if (item.basketId == payload) {
        item.flag = !item.flag;
      }
    });
    state.checkAllFlag = state.cartData.every((item) => {
      return item.flag;
    });
  },
  setPayData(state, payload) {
    // ++ --
    state.cartData.forEach((item) => {
      if (item.basketId == payload.id && payload.option == "+") {
        item.prodCount++;
      } else if (item.basketId == payload.id && payload.option == "-") {
        item.prodCount--;
      }
    });
  },
  settatalPayData(state, payload) {
    // 全选
    state.checkAllFlag = payload;
    state.cartData.forEach((item) => {
      item.flag = payload;
    });
  },
  getTotalCont(state, payload) {
    state.TotalCont = payload;
  },
};
const actions = {
  async getCart({ commit }, paylod) {
    let result = await getCart(paylod);
    if (result) {
      commit("update", result[0]);
    }
  },
  async gettotalPay({ commit }, paylod) {
    let result = await gettotalPay(paylod);
    if (result) {
      commit("gettatalPayData", result);
    }
  },
  async getProdCount({ commit }) {
    let result = await getProdCount();
    if (result) {
      commit("getTotalCont", result);
    }
  },
  async getChageItem({ commit }, payload) {
    let result = await getChageItem(payload);
  },
  async getdeleteItem({ commit }, payload) {
    let result = await getdeleteItem(payload);
  },
};
export default {
  namespaced: true,
  module: {},
  state,
  getters,
  mutations,
  actions,
};
